package connect4client.core;

import game.Player;
import game.PlayerMove;

import java.util.*;

import messages.InvalidInvitationReceivedMessage;

import game.connect4.Connect4Move;
import connect4client.core.Controller;
import connect4client.core.GUIMediator;
import connect4client.net.ConnectionController;
import connect4client.net.servers.Server;

public abstract class AbstractGUIMediator implements GUIMediator {
	
	public AbstractGUIMediator(Controller controller) {
		this.controller = controller;
	}
	

	public abstract void loginFailed();


	public abstract void loginSuccessful();

	
	public abstract void registrationSuccessful();


	public abstract void registrationFailed();

	
	public abstract void userLoggedIn(final Player player);


	public abstract void userLoggedOut(final String playerLogin);


	public abstract void listPlayersReply(final List<Player> players);


	public abstract void wall(final String senderLogin, final String contents);


	public abstract void invitationReceived(final String opponentName, final String gameName, final int replyId);


	public abstract void invitationAccepted(final Player opponent, final String gameName);


	public abstract void invitationDenied(final Player opponent, final String gameName);


	public abstract void invalidInvitationReceived(final InvalidInvitationReceivedMessage.ErrorCode errorCode);


	public abstract void invitationTimedOut(final String login, final String gameType);


	public abstract void matchStarted(final Player me, final Player opponent, final String gameName, final int matchId, final String nextPlayer);


	public abstract void chat(final String login, final String contents, final int matchId);

	public abstract void playerMove(PlayerMove playerMove, int matchId, final String nextPlayer);


	public abstract void invalidMove(int matchId);


	public abstract void giveUp(int matchId);


	public abstract void matchEnded(final int matchId, final List<Player> players, final String winner);


	public abstract void matchEndedTied(int matchId, List<Player> players);


	public void sendLoginMessage(ConnectionController.CommunicationMethod method, Server server, String userName, String password) {
		try {
			controller.getUser().setLogin(userName);
			controller.getUser().setPassword(password);
						
			ConnectionController connectionController = ConnectionController.createInstance(method, controller, server);
			controller.setConnectionController(connectionController);
			
			// Initialize the application and try to contact the server						
			connectionController.login(userName, password);
		} catch (Exception ex) {
			this.loginFailed();
			ex.printStackTrace();
		}
	}
	
	public void sendRegisterMessage(ConnectionController.CommunicationMethod method, Server server, String userName, String password) {
		try {
			controller.getUser().setLogin(userName);
			controller.getUser().setPassword(password);
						
			ConnectionController connectionController = ConnectionController.createInstance(method, controller, server);	
			controller.setConnectionController(connectionController);
			
			// Initialize the application and try to contact the server						
			connectionController.register(userName, password);
		} catch (Exception ex) {
			this.registrationFailed();
			ex.printStackTrace();
		}
	}


	public void sendChatMessage(String opponent, String contents, int matchId) {
		controller.getConnectionController().sendChatMessage(opponent, contents, matchId);
	}


	public void sendPlayerMoveMessage(int matchId, PlayerMove playerMove) {
		controller.getConnectionController().sendPlayerMoveMessage(matchId, playerMove);
	}


	public void sendWallMessage(String contents) {
		controller.getConnectionController().sendWallMessage(contents);
	}


	public void sendInvitationMessage(String login, String gameName) {
		controller.getConnectionController().sendInviteMessage(login, gameName);
	}


	public void sendInvitationReplyMessage(int replyId, boolean accepted) {
		controller.getConnectionController().sendInviteReplyMessage(replyId, accepted);
	}


	public void sendGiveUpMessage(int matchId) {
		controller.getConnectionController().sendGiveUpMessage(matchId);
	}
	
	
	public void sendListPlayersMessage(boolean all) {
		controller.getConnectionController().sendListPlayersMessage(all);
	}
	
	
	public void sendListMatchsMessage() {
		controller.getConnectionController().sendListMatchsMessage();
	}
	
	
	public void sendListGamesMessage() {
		controller.getConnectionController().sendListGamesMessage();
	}
	
	
	public void sendUserDetailsMessage(String login) {
		controller.getConnectionController().sendUserDetailsMessage(login);
	}
	
	
	public void sendAccountCreateMessage(String login, String password, String role) {
		controller.getConnectionController().sendAccountCreateMessage(login, password, role);
	}
	
	
	public void sendAccountEditMessage(String login, String newPassword, String newRole) {
		controller.getConnectionController().sendAccountEditMessage(login, newPassword, newRole);
	}
	
	
	public void sendAccountRemoveMessage(String login) {
		controller.getConnectionController().sendAccountRemoveMessage(login);
	}
	
	
	public void sendListPluginsMessage() {
		controller.getConnectionController().sendListPluginsMessage();
	}
	
	
	public void sendStartPluginMessage(String pluginName) {
		controller.getConnectionController().sendStartPluginMessage(pluginName);
	}
	
	
	public void sendStopPluginMessage(String pluginName) {
		controller.getConnectionController().sendStopPluginMessage(pluginName);
	}


	public void sendQuitMessage() {
		controller.getConnectionController().sendQuitMessage();
		controller.shutDown();
	}

	protected Controller controller;
}
